package server.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPEchoServer {
    private static final int ECHOMAX = 255; // Maximum size of echo datagram
    

    public static void main(String[] args) throws IOException {
//        if (args.length != 1) { // Test for correct argument list
//            throw new IllegalArgumentException("Parameter(s): <Port>");
//        }
//        int servPort = Integer.parseInt(args[0]);
        int servPort = 20008;
        final int N = 5;
        int n=0;
        
        DatagramSocket socket = new DatagramSocket(servPort);
        DatagramPacket packet = new DatagramPacket(new byte[ECHOMAX], ECHOMAX);
        
        while (true) { // Run forever, receiving and echoing datagrams
            socket.receive(packet); // Receive packet from client
            if(n%N==0){
	            socket.send(packet); // Send the same packet back to client
	            System.out.println("Handling client at " + packet.getAddress().getHostAddress() + " on port "
                    + packet.getPort());
            }
            packet.setLength(ECHOMAX); // Reset length to avoid shrinking buffer
            n++;
        }
        /* NOT REACHED */
    }
}